/*
查询
生成工单
统计
导出excel

待完善
 */
/*路径完善后需用组装表头，固定表头需注释掉或删除*/
$(document).ready(function (){
require(['inputdrop',
	     'tree',
	     'tab',
	     'plot',
	     'workorder',
	     'event'] ,function (inputdrop ,tree ,tab ,plot ,workorder ,pevent){
	
	//忽略全部url
	var lgnore_all_url = "performanceEvent/doPerformanceEventIgnoreAll";

	//统计url
	var stat_url = "performanceEvent/statPerformanceEventQuery";

	var el_event_list = $("#table_div");

	var current_header = [];

	event_init();

	pevent.performEventList();

	function event_init()
	{
		// 生成工单
		$("#order_btn").click(function (){
			pevent.createWorkOrder({
				gridEl : $("#table_div"),
				descKey : "perfModule",
				eventIdKey : "performanceNo",
				eventTypeVal : "3"
			});
		});

		//忽略全部
		$("#lgnore_all_btn").click(function(rowData) {
			var data = g_grid.getData($("#table_div") ,{chk:false});
			if (data.length == 0)
			{
				// 弹出提示
				g_dialog.operateAlert($("#table_div") ,"列表无数据。" ,"error");
				// 直接返回
				return false;
			}
			pevent.ignoreAll({
				gridEl: el_event_list,
				lgnore_all_url: "performanceEvent/doPerformanceEventIgnoreAll",
				cb: function() {
					g_grid.refresh(el_event_list);
				}
			});
		});

		//忽略
		$("#lgnore_btn").click(function (rowData){
			pevent.ignore({
				gridEl : el_event_list,
				attr : "performanceNo",
				ignore_url : "performanceEvent/doPerformanceEventIgnore",
				cb: function() {
					g_grid.refresh(el_event_list);
				}
			});
		});

		//统计
		$("#count_btn").click(function (){
			$("#table_div").css("height" ,"auto")
			$("#table_oper").css("border-bottom" ,"none")
			count_table_init();
			$(this).toggle();
			$("#grid_btn").toggle();
		});

		$("#grid_btn").click(function (){
			$("#table_oper").css("border-bottom" ,"1px solid #ddd")
		   //切换到列表展示
			pevent.performEventList();
			$(this).toggle();
			$("#count_btn").toggle();
		});

		//导出EXCEL
		$("#export_btn").click(function (rowData){
			var queryObj = $("#table_div").data("opt").paramObj;

			pevent.eventExport({
				attr : "performanceNo",
				url : "performanceEvent/exportPerformance",
				queryObjStr : JsonTools.encode(queryObj)
			});
		});

		//自定义列
		$("#custom_btn").click(function (rowData){
			pevent.eventCustomCol($(this) ,{
				tplUrl : "module/event_alarm/perform_event_analy_tpl.html",
				colQueryUrl : "performanceEvent/queryProformanColumeIds",
				customColumnsUrl : "performanceEvent/savePerformanceEventCustomColumn",
				type:"3",
				cbf : function (){
					pevent.performEventList();
				}
			});
		});

		index_list_search_render($("um-search") ,{
			cbf : function (val){
				g_grid.refresh($("#table_div") ,{
					paramObj : {perfName : val},
					resetPage : true
				})
			},
			placeholder : "请输入事件名称"
		})

		g_formel.list_filter_render($("um-filter") ,{
			condition : [
							{text : "当前状态" ,name : "currentStatus" ,data : [{text:"正常",value:"1"} ,{text:"异常",value:"0"}]},
							{text : "状态" ,name : "perfStatus" ,data : [{text:"未处理",value:"1"} ,{text:"忽略",value:"2"},
																		 {text:"处理中",value:"3"} ,{text:"已处理",value:"4"}]}
						],
			click : function (paramObj){
				g_grid.refresh($("#table_div") ,{
					paramObj : paramObj
				})
			}
		})
	}

	function count_table_init(){
		el_event_list.empty()
		$.ajax({
			type: "GET",
			url: "module/event_alarm/fault_event_analy_tpl.html",
			success :function(data)
			{
				el_event_list.html($(data).find("[id=count_tem]"));
				chart_render(el_event_list.find("[id=eventname_analy_chars]") ,1);
				chart_render(el_event_list.find("[id=type_analy_chars]") ,2);
				chart_render(el_event_list.find("[id=level_analy_chars]") ,6);
				chart_render(el_event_list.find("[id=time_analy_chars]") ,7);
			}
		});
	}

	function chart_render(el ,typeid)
	{
		var paramObj = new Object();
		paramObj.typeid = typeid;
		if (typeid == "7")
		{
			um_ajax_get({
				url : stat_url,
				paramObj : paramObj,
				maskObj : el,
				successCallBack : function (data){
					var seriesArray = [];
					var categoryArray = [];
					seriesObj = new Object();
					seriesObj.name = "性能事件统计";
					seriesObj.type = "line";
					//seriesObj.areaStyle = {normal: {}};
					seriesObj.data = [];
					var data = data.datas[0].items;
					for (var i = 0; i < data.length; i++) {
						categoryArray.push(data[i].lable);
						seriesObj.data.push(data[i].value);
					}
					seriesArray.push(seriesObj);
					plot.lineRender(el ,{
						category : categoryArray,
						series : seriesArray,
						grid: {
					        left: '6%',
					        right: '9%',
					        bottom: '3%',
					        containLabel: true
					    }
					});
				}
			});
		}
		else
		{
			var color_array = ['#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83',  '#ca8622', '#bda29a','#6e7074', '#546570', '#c4ccd3'];
			um_ajax_get({
					url : stat_url,
					paramObj : paramObj,
					maskObj : el,
					successCallBack : function (data){
						var seriesArray = [];
						var seriesObj = new Object();
						seriesObj.name="值";
						seriesObj.type="bar";
						seriesObj.data=[];
						seriesObj.itemStyle = new Object();
						seriesObj.itemStyle.normal = new Object();
						seriesObj.itemStyle.normal.color = color_array[typeid];
						var categoryArray = [];
						var data = data.datas;
						for (var i = 0; i < data.length; i++) {
							seriesObj.data.push(data[i].value);
							categoryArray.push(data[i].lable);
						}
						seriesArray.push(seriesObj);
						plot.barRender(el ,{
							category : categoryArray,
							series : seriesArray,
							isVercital : true
						});
					}
				});
		}
	}

	function event_record_dbclick(rowData){
		pevent.performEventDetail(rowData);
	}

});
});